﻿using AutoMapper;
using MultipleDB.Entitys;
using MultipleDB.IRepository.Basara;
using MultipleDB.IRepository.UnitOfWork;
using MultipleDB.IServices.Basara;
using PineLake.Drmwe.Lib.EnumText;
using PineLake.Drmwe.Lib.Exceptions;
using PineLake.Drmwe.Lib.ResultMsg;
using PineLake.Entitys.Dto;
using PineLake.Entitys.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MultipleDB.Services.Basara
{
    public class AccountInfo_Services : BaseServices<AccountInfo>, IAccountInfo_Services
    {
        private readonly IAccountInfo_Repository _DAL;
        private readonly IUnitOfWork _UnitOfWork;
        private readonly IMapper _Mapper;
        private ActionResultMsg ActionResult = new();
        public AccountInfo_Services(IUnitOfWork IUnitOfWork, IMapper Mapper, IAccountInfo_Repository IDal)
        {
            _UnitOfWork = IUnitOfWork;
            _Mapper = Mapper;
            _DAL = IDal;
            BaseDal = IDal;
        }

        /// <summary>
        /// 添加  一条数据
        /// </summary>
        /// <param name="AddModel"> Dto</param>
        /// <returns></returns>
        public async Task<ActionResultMsg> AddAsync(Add_AccountInfo_Dto AddModel)
        {
            try
            {
                var AccountInfoModel = _Mapper.Map<AccountInfo>(AddModel);
                if (await _DAL.InsertAsync(AccountInfoModel) <= 0)
                {
                    ActionResult.ActionCode = (int)ActionStatusCodeEnum.Unsuccessful;
                    ActionResult.Info = ActionStatusCodeEnum.Unsuccessful.GetEnumText();
                }
            }
            catch (Exception)
            {
                ActionResult.ActionCode = (int)ActionStatusCodeEnum.Error;
                ActionResult.Info = ActionStatusCodeEnum.Error.GetEnumText();
            }
            return ActionResult;
        }

        /// <summary>
        /// 删除  一条数据,通过主键Id
        /// </summary>
        /// <param name="DeleteDto">Id Dto</param>
        /// <returns></returns>
        public async Task<ActionResultMsg> DeleteAsync(Delete_AccountInfo_ByIdDto DeleteDto)
        {
            try
            {
                if (await _DAL.ExistsAsync(X => X.AccountId == DeleteDto.AccountId))
                {
                    if (!await _DAL.DeleteByIdAsync(DeleteDto.AccountId))
                    {
                        throw new PineLakeException("操作失败,请重新操作!错误代码:XXXXXX", new Exception("IsFalse"));
                    }
                }
                else
                {
                    throw new PineLakeException("操作失败,数据不存在!错误代码:XXXXXX", new Exception("IsFalse"));
                }
            }
            catch (PineLakeException PineEx)
            {
                //var PineExMsg = PineEx.InnerException?.Message;
                //if (!string.IsNullOrEmpty(PineExMsg))
                //{
                //    //如果 需求提交事务 PineEx.InnerException.Message.Equals("IsTrue") 需要回滚 IsFalse 不需要回滚
                //    if (PineExMsg.Equals("IsTrue"))
                //    {
                //        //需求提交事务回滚
                //        //(这里)实现回滚代码
                //    }
                //}
                ActionResult.ActionCode = (int)ActionStatusCodeEnum.Unsuccessful;
                ActionResult.Info = string.IsNullOrEmpty(PineEx.Message) == true ? ActionStatusCodeEnum.Unsuccessful.GetEnumText() : PineEx.Message;
            }
            catch (Exception)
            {
                //需要提交事务回滚
                //(这里)实现回滚代码
                ActionResult.ActionCode = (int)ActionStatusCodeEnum.Error;
                ActionResult.Info = ActionStatusCodeEnum.Error.GetEnumText();
            }
            return ActionResult;
        }

        /// <summary>
        /// 获取一条数据,通过主键Id
        /// </summary>
        /// <param name="GetDto">Id Dto</param>
        /// <returns></returns>
        public async Task<ActionResultMsg> GetAsync(Get_AccountInfo_ByIdDto GetDto)
        {
            // View 必须要指定字段 数据库才能按需读取字段值(请手动指向字段值)
            //var ViewModel = await _DAL.QueryByIdInSelectAsync(GetDto.Id, It => new Get_XXX_ByIdView() { Id = It.Id });
            var ViewModel = await _DAL.QueryByIdInSelectAsync(GetDto.AccountId, It => new Get_AccountInfo_ByIdView());
            ActionResult.Data = ViewModel;
            return ActionResult;
        }

        /// <summary>
        /// 修改 一条数据.通过主键Id
        /// </summary>
        /// <param name="ModifyModel">修改 AccountInfo Dto</param>
        /// <returns></returns>
        public async Task<ActionResultMsg> ModifyAsync(Modify_AccountInfo_Dto ModifyModel)
        {
            try
            {
                //更新请按需更新。不要更新全部数据(请手动指向更新字段值)
                //if (!await _DAL.UpdateAsync(It => new XXX() { Id = ModifyModel.Id, Project = It.Project.Trim() }, X => X.Id == ModifyModel.Id))
                //{
                //throw new PineLakeException("操作失败,系统出错!错误代码:XXXXXX", new Exception("IsFalse"));
                //}
                if (await _DAL.ExistsAsync(X => X.AccountId == ModifyModel.AccountId))
                {
                    var Model = _Mapper.Map<AccountInfo>(ModifyModel);
                    if (!await _DAL.UpdateAsync(Model))
                    {
                        throw new PineLakeException("操作失败,请重新操作!错误代码:XXXXXX", new Exception("IsFalse"));
                    }
                }
                else
                {
                    throw new PineLakeException("操作失败,数据不存在!错误代码:XXXXXX", new Exception("IsFalse"));
                }
            }
            catch (PineLakeException PineEx)
            {
                //var PineExMsg = PineEx.InnerException?.Message;
                //if (!string.IsNullOrEmpty(PineExMsg))
                //{
                //    //如果 需求提交事务 PineEx.InnerException.Message.Equals("IsTrue") 需要回滚 IsFalse 不需要回滚
                //    if (PineExMsg.Equals("IsTrue"))
                //    {
                //        //需求提交事务回滚
                //        //(这里)实现回滚代码
                //    }
                //}
                ActionResult.ActionCode = (int)ActionStatusCodeEnum.Unsuccessful;
                ActionResult.Info = string.IsNullOrEmpty(PineEx.Message) == true ? ActionStatusCodeEnum.Unsuccessful.GetEnumText() : PineEx.Message;
            }
            catch (Exception)
            {
                //需要提交事务回滚
                //(这里)实现回滚代码
                ActionResult.ActionCode = (int)ActionStatusCodeEnum.Error;
                ActionResult.Info = ActionStatusCodeEnum.Error.GetEnumText();
            }
            return ActionResult;
        }
    }
}
